package easy;

import util.PrintUtils;

public class Solution_1122 {

    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        int[] counts = new int[1001];
        for (int i : arr1) {
            counts[i]++;
        }
        int[] arr = new int[arr1.length];
        int index = 0;
        for (int i = 0; i < arr2.length; i++) {
            while (counts[arr2[i]] > 0) {
                arr[index++] = arr2[i];
                counts[arr2[i]]--;
            }
        }
        for (int i = 0; i < counts.length; i++) {
            while (counts[i] > 0) {
                arr[index++] = i;
                counts[i]--;
            }
        }
        return arr;
    }

    public static void main(String[] args) {
        Solution_1122 model = new Solution_1122();
        PrintUtils.arrayInt(
            model.relativeSortArray(new int[]{2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19}, new int[]{2, 1, 4, 3, 9, 6}));
    }
}
